package com.glela.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.common.constant.BaseRestrictionsEnum;
import com.glela.common.constant.ClientType;
import com.glela.common.constant.CommonConstants;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.model.User;
import com.glela.platform.model.request.MobileCode;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.service.CheckCodeService;

@Controller
@RequestMapping(value = "/CheckCode")
public class CheckCodeController {

	private static Logger logger = LoggerFactory.getLogger(InformationCheckController.class);
	
	@Resource
	private CheckCodeService checkCodeService;
	
	@Resource
	private BaseRestrictionsService baseRestrictionsService;
	
	
	/**
	 * 发送验证码
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/sendVerificationCode", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> sendVerificationCode(@RequestBody MobileCode mobileCode,HttpServletRequest request)
	{
		try {
			// 验证参数手机号和类型(注册 找回密码)
			if (mobileCode.getMobile()==null||mobileCode.getType()==null) {
				return new ResponseEntity<ResponseBaseModel>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK); 
			}
			String clientType = ClientType.APP;
			if(request.getHeader("phoneType") != null && request.getHeader("phoneType").length() > 0) {
				if(request.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_IOS) || request.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_ANDROID)){
					//user.setUserMachineCode(request.getHeader("machineCode")==null?"":request.getHeader("machineCode"));
					clientType = ClientType.APP;
				} else if(request.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_h5) || request.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_WX)) {
					clientType = ClientType.H5;
				}
				
			}
			BaseRestrictions baseRestrictions = baseRestrictionsService.getBaseRestrictionsForCache(BaseRestrictionsEnum.APP_LOGIN_FLAG);
			if (baseRestrictions==null || baseRestrictions.getExperience()==1) {
				if ("APP".equals(clientType.toUpperCase())) {
					boolean storeFlag = checkCodeService.checkStoreUser(mobileCode.getMobile());
					if (!storeFlag) {
						return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20276), HttpStatus.OK);
					}
				}
			}
			
			// 发送验证码
			long i = checkCodeService.sendVerificationCode(mobileCode.getMobile(), mobileCode.getType());
			//i>0 发生验证码成功 插入手机校验码表成功
			if (i>0) {
				return new ResponseEntity<ResponseBaseModel>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
			}
			return new ResponseEntity<ResponseBaseModel>(new ResponseBaseModel(ErrorEnum.C20112), HttpStatus.OK); 
		} catch (Exception e) {
			logger.error("发送验证码异常:" , e);
		}
		return new ResponseEntity<ResponseBaseModel>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
	}
	
	/**
	 * 验证验证码是否正确
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/selectCheckCodeByMobile", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> selectCheckCodeByMobile(@RequestBody MobileCode mobileCode)
	{
		try {
			//必传参数
			if (mobileCode.getMobile()==null||mobileCode.getCode()==null) {
				return new ResponseEntity<ResponseBaseModel>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK); 
			}
			int i=checkCodeService.selectCheckCodeByMobile(mobileCode.getMobile(), mobileCode.getCode());
			if (i>0) {
				return new ResponseEntity<ResponseBaseModel>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
			}
			return new ResponseEntity<ResponseBaseModel>(new ResponseBaseModel(ErrorEnum.C20126), HttpStatus.OK); 
		} catch (Exception e) {
			logger.error("验证验证码是否正确:" , e);
		}
		return new ResponseEntity<ResponseBaseModel>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
	}
}
